<?php

/**
 * alltosun.com  index.php
 * ============================================================================
 * 版权所有 (C) 2009-2013 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明：这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 王德康 (wangdk@alltosun.com) $
 * $Date: 2016-1-4 下午2:51:32 $
 * $Id: index.php 165432 2016-02-14 03:55:03Z wangdk $
 */
class Action
{
    private $per_page = 30;

    public function __call($action = '', $params = array()) {
        $order = $page_no = $type = $res_name = $search_filter = null;

        $EX = array(
            'page_no' => 1,
            'type' => 0,
            'res_name' => '',
            'search_filter' => array(),
        );

        extract(URLTracker::init($EX));

        $page = $page_no;


        $filter = array('1' => '1');
        $list = array();

        // 客户端类型
        if ($type != 0) {
            $filter['type'] = $type;
        }
        if ($res_name != '') {
            $filter['res_name'] = $res_name;
        }

        // 主键ID
        if (isset($search_filter['id']) && $search_filter['id']) {
            $filter['id'] = $search_filter['id'];
        }

        // 描述相符
        if (isset($search_filter['txt_desc']) && $search_filter['txt_desc']) {
            $filter['txt_desc LIKE'] = '%' . $search_filter['txt_desc'] . '%';
        }

        // 操作时间
        if (isset($search_filter['start_time']) && $search_filter['start_time']) {
            $filter['update_time >='] = $search_filter['start_time'] . ' 00:00:00';
        }

        if (isset($search_filter['end_time']) && $search_filter['end_time']) {
            $filter['update_time <='] = $search_filter['end_time'] . ' 23:59:59';
        }

        $total = _model('version')->getTotal($filter);

        // 分页
        if ($total) {
            $pager = new Pager($this->per_page);
            $order = 'ORDER BY `id` DESC ';
            $list = _model('version')->getList($filter, $order . $pager->getLimit($page));

            foreach ($list as $k => $v) {
                $list[$k]['txt_desc'] = nl2br($v['txt_desc']);
            }

            if ($pager->generate($total, $page)) {
                Response::assign('pager', $pager);
            }
        }

        Response::assign('search_filter', $search_filter);
        Response::assign('list', $list);
        Response::assign('total', $total);
        Response::assign('type', $type);
        Response::assign('res_name', $res_name);
        Response::display('admin/index.html');
    }

    // 添加版本号
    public function add() {
        Response::assign('app_res_list', version_config::$app_res_list);
        Response::assign('app_type_list', version_config::$app_type_list);

        Response::display('admin/edit.html');
    }

    // 编辑版本号
    public function edit() {

        $id = Request::get('id', 0);

        if (!$id) {
            return '参数错误';
        }

        $info = _uri('version', $id);

        Response::assign('info', $info);
        Response::assign('app_res_list', version_config::$app_res_list);
        Response::assign('app_type_list', version_config::$app_type_list);

        Response::display('admin/edit.html');
    }

    // 保存版本号
    public function save() {
        $id = Request::post('id', 0);
        $info = Request::post('info', array());

        if (!$info) {
            return '信息填写有误!';
        }

        if (strlen($info['txt_desc']) > 900) {
            return '更新说明内容过长';
        }

        if ($id) {
            _model('version')->update($id, $info);
        } else {
            _model('version')->create($info);
        }

        return array(
            '操作成功',
            'success',
            AnUrl('version/admin', '?type=' . $info['type'] . '&res_name=' . $info['res_name'])
        );
    }
}

?>